package com.qf.Servlet;

import com.qf.Service.InformService;
import com.qf.Service.impl.InformServiceImpl;
import com.qf.Utils.JsonUtils;
import com.qf.Utils.ResultVo;
import com.qf.entity.SysNotice;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Timestamp;
import java.util.List;

@WebServlet(name = "informServlet",urlPatterns = "/informServlet")
public class InformServlet extends BaseServlet {
    private InformService informService = new InformServiceImpl();

    public void addNotice(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //接受前端传递的参数
        String id = request.getParameter("id");
        String title = request.getParameter("title");
        String type = request.getParameter("type");
        String content = request.getParameter("content");
        String createtime = request.getParameter("createtime");
        String creater = request.getParameter("createtime");

        SysNotice sysNotice = new SysNotice();
        sysNotice.setId(Integer.parseInt(id));
        sysNotice.setTitle(title);
        sysNotice.setType(Integer.parseInt(type));
        sysNotice.setContent(content);
        sysNotice.setCreatetime(Timestamp.valueOf(createtime));
        sysNotice.setCreater(Long.parseLong(creater));
        int num = informService.addNotice(sysNotice);

        PrintWriter pw = response.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();

        if (num < 0) {
            resultVo.setCode(200);
            resultVo.setMessage("成功");
            resultVo.setData(true);
        }else {
            resultVo.setCode(500);
            resultVo.setMessage("失败");
            resultVo.setData(false);
        }
        JsonUtils.showJson(pw, resultVo);
    }

    public void deleteNotice(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //获取前端参数
        String title = request.getParameter("title");

        PrintWriter pw = response.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();
        int num = informService.deleteNotice(title);
        if (num != -1) {
            resultVo.setCode(200);
            resultVo.setMessage("成功");
            resultVo.setData(true);
        }else {
            resultVo.setCode(500);
            resultVo.setMessage("失败");
            resultVo.setData(false);
        }
        JsonUtils.showJson(pw,resultVo);
    }

    public void selectNotice(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String id = request.getParameter("id");
        List<SysNotice> sysNoticeList = informService.selectNotice(Integer.parseInt(id));

        PrintWriter pw = response.getWriter();

        ResultVo<List<SysNotice>> resultVo = new ResultVo<>();
        if (sysNoticeList != null && sysNoticeList.size() > 0) {
            resultVo.setData(sysNoticeList);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        }else {
            resultVo.setData(null);
            resultVo.setMessage("失败");
            resultVo.setCode(500);
        }
        JsonUtils.showJson(pw, resultVo);
    }

    public void updateNotice(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //接受前端传递的参数
        String title = request.getParameter("title");
        String type = request.getParameter("type");
        String content = request.getParameter("content");
        String createtime = request.getParameter("createtime");
        String creater = request.getParameter("createtime");
        String id = request.getParameter("id");

        SysNotice sysNotice = new SysNotice();
        sysNotice.setTitle(title);
        sysNotice.setType(Integer.parseInt(type));
        sysNotice.setContent(content);
        sysNotice.setCreatetime(Timestamp.valueOf(createtime));
        sysNotice.setCreater(Long.parseLong(creater));
        sysNotice.setId(Integer.parseInt(id));

        int num = informService.updateNotice(sysNotice);
        PrintWriter pw = response.getWriter();
        ResultVo<Boolean> resultVo = new ResultVo<>();
        if (num != -1) {
            resultVo.setData(true);
            resultVo.setMessage("成功");
            resultVo.setCode(200);
        } else {

            resultVo.setData(false);
            resultVo.setMessage("失败");
            resultVo.setCode(500);
        }
        JsonUtils.showJson(pw, resultVo);
    }
}
